package main

import (
	"crypto/rand"
	"crypto/tls"
	"io"
	"log"
	"net"
	"time"
)

func main() {
	cert, err := tls.LoadX509KeyPair("E:/JetBrains/GoglandProjects/pki/src/CARoot.crt",
	"E:/JetBrains/GoglandProjects/pki/src/CARoot.key")
	if err != nil {
		log.Fatalf("server:loadkeys:%s", err.Error())
	}

	config := tls.Config{
		Certificates: []tls.Certificate{cert},
	}
	config.Time = time.Now
	config.Rand = rand.Reader

	service := "127.0.0.1:8000"

	listener, err := tls.Listen("tcp", service, &config)
	if err != nil {
		log.Fatalf("server: listen: %s", err)
	}

	log.Print("server:listenling...")

	for {
		conn, err := listener.Accept()

		if err != nil {
			log.Printf("server: accept: %s", err)
			break
		}

		log.Printf("server:accepted from %s", conn.RemoteAddr())

		go handleClient(conn)
	}
}

func handleClient(conn net.Conn) {
	defer conn.Close()

	buf := make([]byte, 1024)
	for {
		log.Print("server:conn:wating...")
		n, err := conn.Read(buf)
		if err != nil {
			if err != io.EOF {
				log.Printf("server: conn:read:%s", err)
			}
		}

		log.Printf("server: conn:echo %q\n", string(buf[:n]))

		n, err = conn.Write(buf[:n])

		log.Printf("server:conn:wrote %d bytes", n)

		if err != nil {
			log.Printf("server:write :%s", err)
			break
		}
	}
	log.Println("server:conn:closed")
}
